<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" version="-//W3C//DTD XHTML 1.1//EN" dir="ltr">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <script language="javascript" type="text/javascript" src="js/jquery.min.js"></script>
    <script language="javascript" type="text/javascript" src="js/json2.js"></script>
    <script language="javascript" type="text/javascript" src="js/jsonssproxy.js"></script>
    <link href="css/samples.css" rel="stylesheet" type="text/css">
    <title>TimerService demo</title>
</head>
<body><div id="page-content">
 
    <h1>TimerService demo</h1>

    <p class="bar">
        <input id="setTimer" type="button" value="Add a timer" onclick="addTimer();">
    </p>
    <p>
        TimerService (<a href="oneshottimer.php?service-source">view source</a>) exposes two methods:
        <ul>
            <li>SetTimer(timer_id) - sets one-shot timer</li>
            <li>CancelOrUnsetTimer(timer_id) - removes existing timer (both running and expired)</li>
        </ul>
        and three events:
        <ul>
            <li>Created - on server-side timer creation, with timer_id in attached data</li>
            <li>Expired - on server-side timer expiration, with timer_id in attached data</li>
            <li>Removed - on server-side timer deletion, with timer_id in attached data</li>
        </ul>
        All the timers set for 15 seconds. When timer expires, server waits another 15 seconds, then
        deletes it automatically. Events are delivered on service method calls or on periodic
        ping()-ing service object.
    </p>
    <p>
        Currently set timers (click button to add one):
    </p>
    <ul id="timers">
    </ul>

    <div class="cr_footer">Released under GPLv2, (C) 2012 Serge V. Izmaylov</div>

    <script type="text/javascript">
        //<![CDATA[
        var prx;
        var tmrNo = 1;

        function dis() {
            $("#setTimer").attr("disabled", true);
            $(".canceltimer").each(function(i){i.attr("disabled", true);});
        }
        function en() {
            $("#setTimer").removeAttr("disabled");
            $(".canceltimer").each(function(i){i.removeAttr("disabled");});
        }

        dis();

        function addTimer() {
            try {
                prx.TimerService.SetTimer("tmr_"+(tmrNo++));
            } catch (x) {
                if (x instanceof JSONSSError)
                    alert("addTimer fault: " + x.message + "\r\n" + JSON.stringify(x.data));
                else
                    alert("addTimer exception: " + x.toString());
            }
        }
        function pingTimerService() {
            dis();
            try {
                prx.TimerService.ping();
            } catch (x) {}
            en();
            setTimeout(pingTimerService, 1000);
        }

        function removeTimer(tn) {
            try {
                prx.TimerService.CancelOrUnsetTimer(tn);
            } catch (x) {
                if (x instanceof JSONSSError)
                    alert("removeTimer fault: " + x.message + "\r\n" + JSON.stringify(x.data));
                else
                    alert("removeTimer exception: " + x.toString());
            }
        }

        function onCreated(tn) {
            html = "<li id=\"" + tn + "\">Timer " + tn + " <span style=\"font-weight: bold;\">waiting...</span> <input type=\"button\" value=\"Cancel\" onclick=\"removeTimer('" + tn + "');\"/></li>";
            $("#timers").append(html);
        }

        function onExpired(tn) {
            $("#" + tn + " span").text("expired, waiting removal...");
        }

        function onRemoved(tn) {
            $("#" + tn).remove();
        }

        $(document).ready(function () {
            try {
                var serviceurl = location.href;
                serviceurl = serviceurl.replace(/.htm$/, ".php");
                prx = JSONSSProxy.fromUrl(serviceurl);
                prx.TimerService.onCreated = onCreated;
                prx.TimerService.onExpired = onExpired;
                prx.TimerService.onRemoved = onRemoved;
                pingTimerService();
                en();
            } catch (x) {
                if (x instanceof JSONSSError)
                    alert("JSONSSProxy.fromUrl fault: " + x.message + "\r\n" + JSON.stringify(x.data));
                else
                    alert("JSONSSProxy.fromUrl exception: " + x.toString());
            }
        });
        //]]>
    </script>
</div></body>
</html>
